package course;

import java.sql.*;

/**加载驱动类 可以省略
 * 创建链接  DatabaseName=数据库名
 * 创建执行器 预处理执行器
 * 查询
 *获取时间
 * 系统配置
 * 关闭
 */

/**
 * public class Time{
 *         //时分秒属性
 * private int hour;
 * private int minute;
 * private int second;
 * //构造方法
 * public Time(int hour,int minute,int second){
 * this.hour = hour;
 * this.minute = minute;
 * this.second = second;
 * resetTime();
 * }
 * //增改方法
 * public void addHour(int hour){
 * this.hour = this.hour + hour;
 * }
 * public void addMinute(int minute){
 * this.minute = this.minute + minute;
 * resetTime();
 * }
 * public void addSecond(int second){
 * this.second = this.second + second;
 * resetTime();
 * }
 * //防止数据错误，对各项数据进行重新计算。这里能优化，不是最优方法
 * public void resetTime(){
 * int count = hour*60*60+minute*60+second;
 * hour = count/3600;
 * count -= hour*3600;
 * minute = count/60;
 * second = count%60;
 * }
 * //重写toString方法
 * public String toString(){
 * return hour+"时 "+minute+"分 "+second+"秒";
 * }
 * //测试
 * public static void main(String[] args){
 * Time time1 = new Time(2,45,32);
 * Time time2 = new Time(1,75,-32);
 * System.out.println(time1.toString());//输出2时 45分 32秒
 * System.out.println(time2.toString());/
 */
public class Time {
public static void main(String[] args) throws ClassNotFoundException, SQLException {




    // 1. 加载驱动类 可以省略
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    //2.创建链接  DatabaseName=数据库名
    Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=xie", "sa", "123456");
    //3.创建执行器 预处理执行器
    String sql = "select GETDATE()";
    //查询
    PreparedStatement ps = conn.prepareStatement(sql);
    //4.
    ResultSet rs = ps.executeQuery();
    //结果

    while (rs.next()) {
        //
        Timestamp ts = rs.getTimestamp(1);
        //获取时间
        System.out.println(ts);
        //系统配置
    }
    //
    conn.close();
    //关闭
}
};
